<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'普通患者列表'),
	array('op'=>'vip','title'=>'VIP患者列表'),
	array('op'=>'black','title'=>'黑名单'),
	array('op'=>'edit','title'=>'添加患者'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['merchid']=$merchid;
	$params['status']=0;
	$params['isvip']=0;
	if(!empty($_RQ['username'])){
		$params['username like']='%'.trim($_RQ['username']).'%';
		$return['params']['username']=$_RQ['username'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_yiliao_user',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['fromuser']=cfc('core')->member_show($item['fromuser']);
			$item['storetitle']=pdo_getcolumn('jy_yiliao_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']),'title');
			$item['workertitle']=pdo_getcolumn('jy_yiliao_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['fromworkerid']),'name');
			//$user=pdo_get('jy_yiliao_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),array('name','birth','sex','mobile','from_type'));
			//$item['username']=$user['name'];
			//$item['name']=$user['name'];
			$item['mobile']=$user['mobile'];
			
			$item['sex']=$user['sex'];
			$item['birth']=json_decode($user['birth'],true);
			$item['year']=$item['birth']['year'];
			if($item['bodyid']>0){
				$body=pdo_get('jy_yiliao_body',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['bodyid']),array('name','sex','agetime'));
				$item['jrname']=$body['name'];
				$item['sex']=$body['sex'];
				$item['year']=date('Y',$body['agetime']);
			}
			$item['sex']=$item['sex']==1?'男':'女';
			$item['age']=$item['year']>0?date('Y')-$item['year'].'岁':'无';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'id','title'=>'患者ID','type'=>'string'),
			array('name'=>'mid','title'=>'MID','type'=>'string'),
			array('name'=>'member','title'=>'粉丝信息','type'=>'member'),
			array('name'=>'fromuser','title'=>'上级','type'=>'member'),
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'sex','title'=>'性别','type'=>'string'),
			array('name'=>'age','title'=>'年龄','type'=>'string'),
			//array('name'=>'username','title'=>'账号','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'from_type','title'=>'渠道','type'=>'radio','options'=>'from_type'),
			array('name'=>'storetitle','title'=>'来自医馆','type'=>'string'),
			//array('name'=>'workertitle','title'=>'来自大夫','type'=>'string'),
			array('name'=>'status','title'=>'是否拉黑','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'binli','title'=>'自建病历'),
			array('type'=>'modal','op'=>'detail','title'=>'病历信息'),
		)),
		'search'=>array(
			array('name'=>'username','width'=>15,'title'=>'姓名','type'=>'string'),
		)
	);
	$return['from_type']=$this->from_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='vip'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=0;
	$params['isvip']=1;
	if(!empty($_RQ['username'])){
		$params['username like']='%'.trim($_RQ['username']).'%';
		$return['params']['username']=$_RQ['username'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','isvip','viptime','description','status','createtime');
	$list=pdo_getslice('jy_yiliao_user',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$member=model('core/member')->detail($item['mid'],array('credit1','credit2','mobile','groupid','avatar','nickname'));
			$item['member']=$member;
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
		    array('name'=>'id','title'=>'患者ID','type'=>'string'),
			array('name'=>'mid','title'=>'MID','type'=>'string'),
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'username','title'=>'账号','type'=>'string'),
			array('name'=>'member','title'=>'粉丝信息','type'=>'member'),
			array('name'=>'viptime','title'=>'到期时间','type'=>'time'),
			array('name'=>'status','title'=>'是否拉黑','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'username','width'=>15,'title'=>'姓名','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='black'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=1;
	$params['isvip']=1;
	if(!empty($_RQ['username'])){
		$params['username like']='%'.trim($_RQ['username']).'%';
		$return['params']['username']=$_RQ['username'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','isvip','viptime','description','status','createtime');
	$list=pdo_getslice('jy_yiliao_user',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$member=model('core/member')->detail($item['mid'],array('credit1','credit2','mobile','groupid','avatar','nickname'));
			$item['member']=$member;
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'username','title'=>'账号','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'member','title'=>'粉丝信息','type'=>'member'),
			array('name'=>'status','title'=>'是否拉黑','type'=>'switch','et'=>'set'),
		)),
		'search'=>array(
			array('name'=>'username','width'=>15,'title'=>'会员姓名','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='binli'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		
		$postdata['data0'] = $post['data0'];
		$postdata['data1'] = $post['data1'];
		$postdata['data2'] = $post['data2'];
		$postdata['data3'] = $post['data3'];
		$postdata['data4'] = $post['data4'];
        $postdata['data5'] = $post['data5'];
		
		$body=pdo_get('jy_yiliao_body',array('id'=>$post['bodyid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));
    	$body['jianli']=json_decode($body['jianli'],true);
    	$body['jianli'][]=$postdata;
    	$data =array(
    		'jianli'=>ijson_encode($body['jianli']),
    	);
		pdo_update('jy_yiliao_body',$data,array('id'=>$post['bodyid'],'uniacid'=>$_SESSION['uniacid']));
		exi('建立成功！','','list');
	}
	$item=$this->get('user',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定患者不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'添加病历',
		'edit'=>array(
		    array('name'=>'data0','title'=>'患者主诉','type'=>'text'),
			array('name'=>'data1','title'=>'诊断','type'=>'text'),
			array('name'=>'data2','title'=>'处方治疗','type'=>'text'),
			array('name'=>'data3','title'=>'过敏/手术病史','type'=>'string'),
			array('name'=>'data4','title'=>'长期慢性病','type'=>'string'),
			array('name'=>'data5','title'=>'最近用药','type'=>'string'),
			//array('type'=>'image','name'=>'thumb','title'=>'照片'),
		),
	);
	exi($return);
}


if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	$item2=$this->get('user',array('id'=>$id));
	$item=$this->get('body',array('id'=>$item2['bodyid']));
    $item['jianli']=json_decode($item['jianli'],true);
 
	$return['item']=$item;	
	$htmls=array(
		'type'=>'look',
		'desc'=>'病历信息',
	);
	$htmls['look']=array(
			array(
				'title'=>'病历',
				'type'=>'array',
				'name'=>'jianli',
				'list'=>array(
					array('type'=>'string','name'=>'data0','title'=>'患者主诉'),	
					array('type'=>'string','name'=>'data1','title'=>'诊断'),
					array('type'=>'string','name'=>'data2','title'=>'处方治疗'),
					array('type'=>'string','name'=>'data3','title'=>'过敏/手术病史'),
					array('type'=>'string','name'=>'data4','title'=>'长期慢性病'),
					array('type'=>'string','name'=>'data5','title'=>'最近用药'),
				),
			)
	);
	$return['htmls']=$htmls;
	exi($return);
}


if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yiliao_user',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'name','title'=>'姓名','type'=>'string'),
					array('name'=>'mobile','title'=>'手机号','type'=>'string'),
					array('name'=>'username','title'=>'账号','type'=>'string'),
					array('name'=>'password','title'=>'密码','type'=>'password'),
					array('name'=>'area_id','title'=>'所属区域','type'=>'select','options'=>'areas'),
					array('name'=>'storeid','title'=>'所属医院','type'=>'select','options'=>'stores'),
					//关联用户ID
			        array('name'=>'mid','title'=>'关联用户ID','type'=>'choose','action'=>'jy_merch/get_member/')
				)
			),
		)
	);
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['areas']=pdo_getall('jy_yiliao_area',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid));
	$return['stores']=pdo_getall('jy_yiliao_store',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid));
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['area_id']){
		exi('请选择区域','error');
	}
	if(!$postdata['storeid']){
		exi('请选择医馆','error');
	}
	//$postdata['time']=ijson_encode($postdata['time']);	
	$postdata['merchid'] = $merchid;
	$data=array_fields(array('name','username','password','mobile','area_id','storeid','merchid'),$postdata);
	$id=intval($postdata['id']);	
	if($id){
		pdo_update('jy_yiliao_user',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_yiliao_user',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_yiliao_user',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的患者！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_yiliao_user',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}